package com.artron.mediaartron.data.production;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.artron.mediaartron.data.entity.DefaultAddressData;
import com.artron.mediaartron.data.entity.FrameData;
import com.artron.mediaartron.data.entity.Works;
import com.artron.mediaartron.ui.widget.TransformCustomImageView;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-29.
 */

public class SuitableSizeHelper {

    private static SuitableSizeHelper sHelper = null;
    private Matrix mMatrix;
    private Bitmap mClipBitmap;
    private FrameData mFrameData;
    private float mScaleFactor;
    private Works mWorks;
    private DefaultAddressData mDefaultAddress;
    private String orderId;
    private boolean isNotStart;
    private boolean isStartPayDetail = true;

    private SuitableSizeHelper(){}
    public static SuitableSizeHelper getHelper() {
        if (sHelper == null) {
            synchronized (SuitableSizeHelper.class) {
                if (sHelper == null) {
                    sHelper = new SuitableSizeHelper();
                }
            }
        }
        return sHelper;
    }

    public boolean isStartPayDetail() {
        return isStartPayDetail;
    }

    public void setStartPayDetail(boolean startPayDetail) {
        isStartPayDetail = startPayDetail;
    }

    public void setMatrix(Matrix matrix) {
        this.mMatrix = matrix;
    }

    public float getScaleFactor() {
        return mScaleFactor;
    }

    public void setScaleFactor(float scaleFactor) {
        mScaleFactor = scaleFactor;
    }

    public Matrix getMatrix() {
        return mMatrix;
    }

    public Bitmap getClipBitmap() {
        return mClipBitmap;
    }

    public void setClipBitmap(Bitmap clipBitmap) {
        mClipBitmap = clipBitmap;
    }

    public FrameData getFrameData() {
        return mFrameData;
    }

    public void setFrameData(FrameData frameData) {
        mFrameData = frameData;
    }

    public Works getWorks() {
        if (mWorks == null) {
            mWorks = new Works();
        }
        return mWorks;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public boolean isNotStart() {
        return isNotStart;
    }

    public void setWorks(Works works) {
        mWorks = works;
    }

    public DefaultAddressData getDefaultAddress() {
        return mDefaultAddress;
    }

    public void setDefaultAddress(DefaultAddressData defaultAddress) {
        mDefaultAddress = defaultAddress;
    }

    public void release() {
        if (sHelper != null) {
            try {
                sHelper.finalize();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            } finally {
                sHelper = null;
                mMatrix = null;
                if (mClipBitmap != null) {
                    mClipBitmap.recycle();
                }
            }
        }

    }

    public boolean isSuitableSize(float widthMM, float heightMM,
                                         float imageWidth, float imageHeight) {
        //最高宽高值
        float lowestHeight = heightMM / FrameConstant.INCH_PER * FrameConstant.LOWEST_DPI;
        float lowestWidth = widthMM / FrameConstant.INCH_PER * FrameConstant.LOWEST_DPI;

        Log.d(TAG, "LowestHeight: " + lowestHeight + "\tLowestWidth：" + lowestWidth
                + "\tImageHeight：" + imageHeight + "\tImageWidth" + imageWidth);
        if (imageWidth < lowestWidth || imageHeight < lowestHeight) {
            return false;
        }

        return true;
    }

    public float[] getImagePercentSize(TransformCustomImageView iv, float degree) {
        Drawable drawable = iv.getDrawable();
        float scale = iv.getScaleFactor();
        float scaleHeight = scale * drawable.getIntrinsicHeight();
        float scaleWidth = scale * drawable.getIntrinsicWidth();
        RectF rectF = iv.getImageRectF();
        float percentLeft = 0.f;
        float percentTop = 0.f;
        float percentRight = 0.f;
        float percentBottom = 0.f;
        if (degree == 0.f || Math.abs(degree) == 180.f) {
            percentLeft = Math.abs(rectF.left) / scaleWidth;
            percentTop = Math.abs(rectF.top) / scaleHeight;
            percentRight = (Math.abs(rectF.left) + iv.getWidth()) / scaleWidth;
            percentBottom = (Math.abs(rectF.top) + iv.getHeight()) / scaleHeight;
        } else if(Math.abs(degree) == 90.f) {
            percentLeft = Math.abs(rectF.left) / scaleHeight;
            percentTop = Math.abs(rectF.top) / scaleWidth;
            percentRight = (Math.abs(rectF.left) + iv.getWidth()) / scaleHeight;
            percentBottom = (Math.abs(rectF.top) + iv.getHeight()) / scaleWidth;
        }
        Log.d(TAG, "Result——left：" + percentLeft + "\ttop：" + percentTop + "\tright：" + percentRight
                +"\tbottom：" + percentBottom + "\tDegree：" + degree);
        return new float[]{percentLeft, percentTop, percentRight, percentBottom};
    }

    public float[] getImageSize(TransformCustomImageView iv, float degree) {
        Drawable drawable = iv.getDrawable();
        if (drawable == null) {
            return new float[]{0, 0, 0 ,0};

        }
        float[] imagePercentSize = getImagePercentSize(iv, degree);
        float imageWidth = 0;
        float imageHeight = 0;
        float x = 0;
        float y = 0;

        Log.d(TAG, "DrawableSize: （" + drawable.getIntrinsicWidth() + ", " + drawable.getIntrinsicHeight() + ")"
            + "\tScaleFactor：" + iv.getScaleFactor());

        if (degree == 0.f || Math.abs(degree) == 180.f) {
            x = drawable.getIntrinsicWidth() * imagePercentSize[0];
            y = drawable.getIntrinsicHeight() * imagePercentSize[1];
            imageWidth = drawable.getIntrinsicWidth() * (fixPercentSize(imagePercentSize[2] - imagePercentSize[0]));
            imageHeight = drawable.getIntrinsicHeight() * (fixPercentSize(imagePercentSize[3] - imagePercentSize[1]));

            if (x + imageWidth > drawable.getIntrinsicWidth()) {
                x = drawable.getIntrinsicWidth() - imageWidth;
            }
            if (y + imageHeight > drawable.getIntrinsicHeight()) {
                y = drawable.getIntrinsicHeight() - imageHeight;
            }
        } else if(Math.abs(degree) == 90.f) {
            x = drawable.getIntrinsicHeight() * imagePercentSize[0];
            y = drawable.getIntrinsicWidth() * imagePercentSize[1];
            imageWidth = drawable.getIntrinsicHeight() * (fixPercentSize(imagePercentSize[2] - imagePercentSize[0]));
            imageHeight = drawable.getIntrinsicWidth() * (fixPercentSize(imagePercentSize[3] - imagePercentSize[1]));

            if (x + imageWidth > drawable.getIntrinsicHeight()) {
                x = drawable.getIntrinsicHeight() - imageWidth;
            }
            if (y + imageHeight > drawable.getIntrinsicWidth()) {
                y = drawable.getIntrinsicWidth() - imageHeight;
            }
        }

        return new float[]{x, y, imageWidth, imageHeight};
    }

    private float fixPercentSize(float size) {
        if (size > 1) {
            return 1.f;
        } else {
            return size;
        }
    }

    public float getSuitableDegree(float degree) {
        int intDegree = (int) degree;
        int absDegree = Math.abs(intDegree);
        if (absDegree >= 0 && absDegree <= 45) {
            absDegree = 0;
        } else if (absDegree > 45 && absDegree <= 135) {
            absDegree = intDegree < 0 ? -90 : 90;
        } else {
            absDegree = 180;
        }
        return absDegree;
    }

    public void setNotStart(boolean notStart) {
        isNotStart = notStart;
    }

}
